package models

import (
	"fmt"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

//定时结算Boottime表数据
func BoottimeTimingSettlement() {
	fmt.Printf("开启定时任务 : %v\n", time.Now())
	for {
		now := time.Now()
		// 计算下一个零点
		next := now.Add(time.Hour * 24)
		next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location())
		t := time.NewTimer(next.Sub(now))
		<-t.C
		fmt.Printf("定时结算Boottime表数据，结算完成: %v\n", time.Now())
		//以下为定时执行的操作
		BoottimeSettlement()
	}
}
func BoottimeSettlement() {
	UpdateRateTask()
}

func UpdateRateTask() {
	o := orm.NewOrm()
	o.Begin()
	sql := `UPDATE sf_rate SET rate= 5, "content"='非常满意' 
WHERE 
rate = 0 AND
date_part('day',  now()- ctime) > 2;`
	_, err := o.Raw(sql).Exec()
	if err != nil {
		beego.Debug(err)
		o.Rollback()
	}
	o.Commit()
}